package com.caigou.modules.channel.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.channel.bean.*
import com.caigou.modules.channel.repository.ChannelRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/10/12
 */
@HiltViewModel
class ChannelViewModel @Inject constructor(private val repository: ChannelRepository) : ViewModel() {

    val teamListLiveData = MutableLiveData<Resource<TeamBean>>()
    val medalListLiveData = MutableLiveData<Resource<MedalBean>>()
    val blackHouseLiveData = MutableLiveData<Resource<BannedBean>>()
    val friendLiveData = MutableLiveData<Resource<FriendBean>>()
    val followLiveData = MutableLiveData<Resource<FollowBean>>()
    val blackLiveData = MutableLiveData<Resource<BlackBean>>()
    val receiveMedalLiveData = MutableLiveData<Resource<String>>()
    val wearMedalLiveData = MutableLiveData<Resource<String>>()

    fun getTeamList(page: Int, limit: Int, userId: String) {
        teamListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getTeamList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<TeamBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    teamListLiveData.value = it
                }
        }
    }

    fun getMedalList(userId: String) {
        medalListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMedalList(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MedalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }.collectLatest {
                    medalListLiveData.value = it
                }
        }
    }

    fun getBlackHouseInfo(userId: String) {
        blackHouseLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getBlackHouseInfo(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BannedBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    blackHouseLiveData.value = it
                }
        }
    }


    fun getFriendList(page: Int, limit: Int, userId: String) {
        friendLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFriendList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FriendBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    friendLiveData.value = it
                }

        }
    }

    fun getFollowList(page: Int, limit: Int, userId: String) {
        followLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFollowList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FollowBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    followLiveData.value = it
                }
        }
    }

    fun getBlackList(page: Int, limit: Int, userId: String) {
        blackLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getBlackList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BlackBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    blackLiveData.value = it
                }
        }
    }

    fun receiverMedal(medalId: String, userId: String) {
        receiveMedalLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.receiveMedal(medalId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BlackBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    receiveMedalLiveData.value = it
                }
        }
    }

    fun wearMedal(wearStatus: Int, medalId: String, userId: String) {
        wearMedalLiveData.value = Resource.Loading()
        viewModelScope.launch {
            if (wearStatus == 0) {
                repository.wearMedal(medalId, userId)
                    .map {
                        if (it.code == 0) {
                            Resource.Success(it.msg)
                        } else {
                            Resource.DataError(it.code, it.msg)
                        }
                    }
                    .catch {
                        val message = if (TextUtils.isEmpty(it.message)) {
                            ""
                        } else {
                            it.message!!
                        }
                        Resource.DataError<String>(-1, message)
                        Logger.e("catch: ${it.message}")
                    }
                    .collectLatest {
                        wearMedalLiveData.value = it
                    }
            } else {
                repository.takeOffMedal(medalId, userId)
                    .map {
                        if (it.code == 0) {
                            Resource.Success(it.msg)
                        } else {
                            Resource.DataError(it.code, it.msg)
                        }
                    }
                    .catch {
                        val message = if (TextUtils.isEmpty(it.message)) {
                            ""
                        } else {
                            it.message!!
                        }
                        Resource.DataError<String>(-1, message)
                        Logger.e("catch: ${it.message}")
                    }
                    .collectLatest {
                        wearMedalLiveData.value = it
                    }
            }
        }
    }

}